package net.xuzhen97.distributed.transaction.course.adpter.rocketmq;

import lombok.extern.slf4j.Slf4j;
import net.xuzhen97.distributed.transaction.course.service.dto.OpenCourseCmd;
import net.xuzhen97.distributed.transaction.course.service.executor.OpenCourseCmdExe;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 开通课程消息监听
 * @author xuzhen97
 */
@Component
@RocketMQMessageListener(topic = "tx_order", consumerGroup = "${rocketmq.consumer.group}"
        , selectorExpression = "pay-complete", selectorType = SelectorType.TAG)
@Slf4j
public class OpenCourseListener implements RocketMQListener<OpenCourseMessage> {

    private final OpenCourseCmdExe openCourseCmdExe;

    @Autowired
    public OpenCourseListener(OpenCourseCmdExe openCourseCmdExe){
        this.openCourseCmdExe = openCourseCmdExe;
    }

    @Override
    public void onMessage(OpenCourseMessage openCourseMessage) {
        log.info("事务执行了 trxId ：{}。", openCourseMessage.getTrxId());
        OpenCourseCmd openCourseCmd = new OpenCourseCmd();
        openCourseCmd.setCourseId(openCourseMessage.getCourseId());
        openCourseCmd.setUserId(openCourseMessage.getUserId());
        openCourseCmd.setOpUserId(openCourseMessage.getOpUserId());
        openCourseCmdExe.execute(openCourseCmd);
    }
}
